package com.kotlin.myapplication.mvp.ui.main.fragment.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.myapplication.R
import kotlinx.android.synthetic.main.item_recyclerviw.view.*

/**
 *@lil
 *@创建时间 2020/4/28 11:37
 *@描述
 */

class RecyclerAdapter(var list: MutableList<String>, var context: Context) : RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView = LayoutInflater.from(context).inflate(R.layout.item_recyclerviw, parent, false)
        var holder = MyViewHolder(itemView)
        itemView.setOnClickListener {
            onClick!!.OnItemClick(itemView, itemView.tag as Int)
        }
        return holder
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.itemView.textview.text = list[position]
        holder.itemView.tag = position
        when (position % 2) {
            0 -> holder.itemView.setBackgroundColor(context.resources.getColor(R.color.colorAccent))
            else -> holder.itemView.setBackgroundColor(context.resources.getColor(R.color.colorPrimaryDark))
        }
    }


    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun MyViewHolder() {

        }
    }
    var onClick: OnItemClickListener? = null

    fun setOnItemClick(onItemClickListener: OnItemClickListener) {
        this.onClick = onItemClickListener
    }
    interface OnItemClickListener {
        fun OnItemClick(view: View, position: Int)
    }


}




